<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="商品名称" prop="skuName">
        <el-input v-model="formData.skuName" placeholder="请输入商品名称" />
      </el-form-item>
      <el-form-item label="商户商品编码" prop="customerSkuCode">
        <el-input v-model="formData.customerSkuCode" placeholder="请输入商户商品编码" />
      </el-form-item>
      <el-form-item label="居然商品编码" prop="iotSkuCode">
        <el-input v-model="formData.iotSkuCode" placeholder="请输入居然商品编码" />
      </el-form-item>
      <el-form-item label="厂家商品编码" prop="factorySkuCode">
        <el-input v-model="formData.factorySkuCode" placeholder="请输入厂家商品编码" />
      </el-form-item>
      <el-form-item label="居然商品名称" prop="iotSkuName">
        <el-input v-model="formData.iotSkuName" placeholder="请输入居然商品名称" />
      </el-form-item>
      <el-form-item label="商品品牌" prop="skuBrand">
        <el-input v-model="formData.skuBrand" placeholder="请输入商品品牌" />
      </el-form-item>
      <el-form-item label="商品单位" prop="skuUnit">
        <el-input v-model="formData.skuUnit" placeholder="请输入商品单位" />
      </el-form-item>
      <el-form-item label="所属客户" prop="customerCode">
        <el-input v-model="formData.customerCode" placeholder="请输入所属客户" />
      </el-form-item>
      <el-form-item label="是否定制品" prop="customizedInd">
        <el-radio-group v-model="formData.customizedInd">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="长（MM）" prop="length">
        <el-input v-model="formData.length" placeholder="请输入长（MM）" />
      </el-form-item>
      <el-form-item label="宽（MM）" prop="width">
        <el-input v-model="formData.width" placeholder="请输入宽（MM）" />
      </el-form-item>
      <el-form-item label="高（MM）" prop="height">
        <el-input v-model="formData.height" placeholder="请输入高（MM）" />
      </el-form-item>
      <el-form-item label="毛重（KG）" prop="grossWeight">
        <el-input v-model="formData.grossWeight" placeholder="请输入毛重（KG）" />
      </el-form-item>
      <el-form-item label="体积（立方米）" prop="volume">
        <el-input v-model="formData.volume" placeholder="请输入体积（立方米）" />
      </el-form-item>
      <el-form-item label="商品一级分类" prop="firstCategoryLevel">
        <el-input v-model="formData.firstCategoryLevel" placeholder="请输入商品一级分类" />
      </el-form-item>
      <el-form-item label="商品二级分类" prop="secondCategoryLevel">
        <el-input v-model="formData.secondCategoryLevel" placeholder="请输入商品二级分类" />
      </el-form-item>
      <el-form-item label="商品三级分类" prop="thirdCategoryLevel">
        <el-input v-model="formData.thirdCategoryLevel" placeholder="请输入商品三级分类" />
      </el-form-item>
      <el-form-item label="启停状态" prop="disabled">
        <el-radio-group v-model="formData.disabled">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="数据来源" prop="dataSource">
        <el-input v-model="formData.dataSource" placeholder="请输入数据来源" />
      </el-form-item>
      <el-form-item label="商品引用时间" prop="referenceTime">
        <el-date-picker
          v-model="formData.referenceTime"
          type="date"
          value-format="x"
          placeholder="选择商品引用时间"
        />
      </el-form-item>
      <el-form-item label="市场参考价" prop="referencePrice">
        <el-input v-model="formData.referencePrice" placeholder="请输入市场参考价" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CommodityApi, CommodityVO } from '@/api/mdm/commodity'

/** 商品信息 表单 */
defineOptions({ name: 'CommodityForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  skuName: undefined,
  customerSkuCode: undefined,
  iotSkuCode: undefined,
  factorySkuCode: undefined,
  iotSkuName: undefined,
  skuBrand: undefined,
  skuUnit: undefined,
  customerCode: undefined,
  customizedInd: undefined,
  length: undefined,
  width: undefined,
  height: undefined,
  grossWeight: undefined,
  volume: undefined,
  firstCategoryLevel: undefined,
  secondCategoryLevel: undefined,
  thirdCategoryLevel: undefined,
  disabled: undefined,
  dataSource: undefined,
  referenceTime: undefined,
  referencePrice: undefined,
})
const formRules = reactive({
  skuName: [{ required: true, message: '商品名称不能为空', trigger: 'blur' }],
  customerSkuCode: [{ required: true, message: '商户商品编码不能为空', trigger: 'blur' }],
  iotSkuCode: [{ required: true, message: '居然商品编码不能为空', trigger: 'blur' }],
  factorySkuCode: [{ required: true, message: '厂家商品编码不能为空', trigger: 'blur' }],
  iotSkuName: [{ required: true, message: '居然商品名称不能为空', trigger: 'blur' }],
  skuBrand: [{ required: true, message: '商品品牌不能为空', trigger: 'blur' }],
  skuUnit: [{ required: true, message: '商品单位不能为空', trigger: 'blur' }],
  customerCode: [{ required: true, message: '所属客户不能为空', trigger: 'blur' }],
  firstCategoryLevel: [{ required: true, message: '商品一级分类不能为空', trigger: 'blur' }],
  secondCategoryLevel: [{ required: true, message: '商品二级分类不能为空', trigger: 'blur' }],
  thirdCategoryLevel: [{ required: true, message: '商品三级分类不能为空', trigger: 'blur' }],
  dataSource: [{ required: true, message: '数据来源不能为空', trigger: 'blur' }],
  referenceTime: [{ required: true, message: '商品引用时间不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CommodityApi.getCommodity(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CommodityVO
    if (formType.value === 'create') {
      await CommodityApi.createCommodity(data)
      message.success(t('common.createSuccess'))
    } else {
      await CommodityApi.updateCommodity(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    skuName: undefined,
    customerSkuCode: undefined,
    iotSkuCode: undefined,
    factorySkuCode: undefined,
    iotSkuName: undefined,
    skuBrand: undefined,
    skuUnit: undefined,
    customerCode: undefined,
    customizedInd: undefined,
    length: undefined,
    width: undefined,
    height: undefined,
    grossWeight: undefined,
    volume: undefined,
    firstCategoryLevel: undefined,
    secondCategoryLevel: undefined,
    thirdCategoryLevel: undefined,
    disabled: undefined,
    dataSource: undefined,
    referenceTime: undefined,
    referencePrice: undefined,
  }
  formRef.value?.resetFields()
}
</script>